package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.bean.ClassMate;
import com.example.myapplication.utli.SPS;
import com.example.myapplication.utli.UtilsEnum;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.myapplication.utli.UtilsEnum.URL;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    private EditText account;
    private EditText passward;
    private Button enter;
    private Button zuceBtn;
    private String userStr;
    private String pwdStr;
    private CheckBox checkBox;
    private Button wangjimima_bnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvent();
    }

    private void initView() {
        account = findViewById(R.id.account_et);
        passward = findViewById(R.id.passward_et);
        enter = findViewById(R.id.enter_btn);
        zuceBtn = findViewById(R.id.zuce_bnt);
        checkBox = findViewById(R.id.box);
        wangjimima_bnt = findViewById(R.id.wangjimima_bnt);

        String usernameStr = SPS.getSaveString(LoginActivity.this, "name");
        String passwordStr = SPS.getSaveString(LoginActivity.this, "pwd");

        if (usernameStr != null && passwordStr != null) {
            account.setText(usernameStr);
            passward.setText(passwordStr);
        }
    }

    private void initData() {
        String url = "http://anli-wechat.boruiman.com/freshmenapp/user/loginhttp://anli-wechat.boruiman.com/freshmenapp/user/login";
        String nameString = account.getText().toString().trim();
        String passwordStr = passward.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "");
            jsonObject.put("password", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG,"onResponse"+jsonObject.optString("msg"));
                Toast.makeText(LoginActivity.this,jsonObject.optString("msg"),Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
               Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
            }
        });
    }


        private void initEvent() {
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initData();
                //TODO 访问服务端登录
                RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                JSONObject jsonRequest = new JSONObject();
                try {
                    jsonRequest.put("name", userStr);
                    jsonRequest.put("password", pwdStr);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
                        URL.url + "user/login?name=" + userStr + "&password=" + pwdStr,
                        jsonRequest, (response -> {
                    String responseString = response.toString();
                    Gson gson = new Gson();
                    ClassMate data = gson.fromJson(responseString, ClassMate.class);
                    if (data.getData() != null) {
                        if (checkBox.isChecked()) {
                            SPS.SaveString(LoginActivity.this, "name", data.getData().getName());
                            SPS.SaveString(LoginActivity.this, "pwd", data.getData().getPassword());
                        } else {
                            SPS.destory(LoginActivity.this);
                        }
                        UtilsEnum.ID = data.getData().getId();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }


                    Log.e(TAG, "Login: " + data);

                }), (error -> {

                }));

                queue.add(request);
            }
        });
        zuceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent1);
            }
        });
        wangjimima_bnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        });
    }
}